package net.thermetics.fx.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import javax.jdo.annotations.IdentityType;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;

import org.springframework.beans.factory.annotation.Autowired;

import net.thermetics.fx.auth.GaeUser;
import net.thermetics.fx.services.UserService;

@PersistenceCapable(identityType = IdentityType.APPLICATION, detachable = "true")
public class Report extends ModelObject {
	@Persistent
	private String threadID;
	
	@Persistent(defaultFetchGroup = "true")
	private List<ReportVote> votes = new ArrayList<ReportVote>();
	
	public String getThreadID() {
		return threadID;
	}
	
	public void setThreadID(String id) {
		threadID = id;
	}
	
	public List<ReportVote> getVotes() {
		return Collections.unmodifiableList(votes);
	}
	
	public void setVotes(List<ReportVote> votes) {
		this.votes = votes;
	}
	
	public void addVote(ReportVote vote) {
		votes.add(vote);
	}
	
	public boolean removeVote(ReportVote vote) {
		votes.remove(vote);
		return true;
	}
	
	public ReportVote getVoteForUser(GaeUser user) {
		for (ReportVote vote : getVotes()) {
			if (vote.getUserKey().equals(user.getKey())) {
				return vote;
			}
		}
		
		return null;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((threadID == null) ? 0 : threadID.hashCode());
		result = prime * result + ((votes == null) ? 0 : votes.hashCode());
		return result;
	}

	/* (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Report other = (Report) obj;
		if (threadID == null) {
			if (other.threadID != null)
				return false;
		} else if (!threadID.equals(other.threadID))
			return false;
		if (votes == null) {
			if (other.votes != null)
				return false;
		} else if (!votes.equals(other.votes))
			return false;
		return true;
	}

}